package com.rockbb.common.utilities.text;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonUtils 
{
	protected static Logger logger = Logger.getLogger(JacksonUtils.class);

	public static <T> T extractObject(String json, Class<T> valueType)
	{
		if (json == null || json.equals("")) return null;

		T output = null;
		ObjectMapper mapper = JacksonMapper.getInstance();

		try 
		{
			output = mapper.readValue(json, valueType);
		} 
		catch (JsonParseException e) 
		{
			logger.info("JsonParseException");
			logger.debug(e.getMessage(), e);
		} 
		catch (IOException e) 
		{
			logger.info("IOException");
			logger.debug(e.getMessage(), e);
		}
		return output;
	}

	public static <T> String compressObject(T object)
	{
		String json = "";
		ObjectMapper mapper = JacksonMapper.getInstance();
		try 
		{
			json = mapper.writeValueAsString(object);
		}
		catch (JsonGenerationException e) 
		{
			logger.info("JsonGenerationException");
			logger.debug(e.getMessage(), e);
		} 
		catch (IOException e) 
		{
			logger.info("IOException");
			logger.debug(e.getMessage(), e);
		}
		return json;
	}

	public static <T> List<T> extractList(String listString, List<T> list) 
	{
		if (listString == null || listString.length()==0) return list;

		List<T> output = null;
		ObjectMapper mapper = JacksonMapper.getInstance();
		try 
		{
			output = mapper.readValue(listString, list.getClass());
		} 
		catch (JsonParseException e) 
		{
			logger.info("JsonParseException");
			logger.debug(e.getMessage(), e);
		} 
		catch (IOException e) 
		{
			logger.info("IOException");
			logger.debug(e.getMessage(), e);
		}
		return (output == null)? list : output;
	}

	public static <T> String compressList(List<T> list) 
	{
		String listString = "";
		ObjectMapper mapper = JacksonMapper.getInstance();
		try 
		{
			listString = mapper.writeValueAsString(list);
		} 
		catch (JsonGenerationException e) 
		{
			logger.info("JsonGenerationException");
			logger.debug(e.getMessage(), e);
		} 
		catch (IOException e) 
		{
			logger.info("IOException");
			logger.debug(e.getMessage(), e);
		}
		return listString;
	}

	public static <T, S> Map<T, S> extractMap(String mapString, Map<T, S> map) 
	{
		if (mapString == null || mapString.equals("")) return map;

		Map<T, S> output = null;
		ObjectMapper mapper = JacksonMapper.getInstance();
		try 
		{
			output = mapper.readValue(mapString, map.getClass());
		}
		catch (JsonParseException e) 
		{
			logger.info("JsonParseException");
			logger.debug(e.getMessage(), e);
		} 
		catch (IOException e) 
		{
			logger.info("IOException");
			logger.debug(e.getMessage(), e);
		}
		return (output == null)? map : output;
	}

	public static <T, S> String compressMap(Map<T, S> map) 
	{
		String mapString = "";
		ObjectMapper mapper = JacksonMapper.getInstance();
		try {
			mapString = mapper.writeValueAsString(map);
		}
		catch (JsonGenerationException e) 
		{
			logger.info("JsonGenerationException");
			logger.debug(e.getMessage(), e);
		} 
		catch (IOException e) 
		{
			logger.info("IOException");
			logger.debug(e.getMessage(), e);
		}
		return mapString;
	}

	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("html_content", "1");
		map.put("css_content", "2");
		String output = compressMap(map);
		System.out.println(output);
	}
}
